package com.start.shop.api.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.start.shop.api.param.order.OrderConfirmParam;
import com.start.shop.api.param.order.OrderMakeParam;
import com.start.shop.api.param.order.OrderQueryParam;
import com.start.shop.api.param.order.PayParam;
import com.start.shop.api.vo.coupon.CouponVo;
import com.start.shop.api.vo.order.OrderInfoVo;
import com.start.shop.api.vo.order.OrderQueryVo;
import com.start.shop.api.vo.order.OrderVo;
import com.start.shop.common.constant.MsgObject;
import com.start.shop.modules.repository.entity.Goods;
import com.start.shop.modules.repository.entity.GoodsType;
import com.start.shop.modules.repository.entity.Order;
import com.start.shop.modules.repository.entity.Sku;
import com.start.shop.modules.repository.entity.User;
import com.start.shop.modules.service.ICouponService;
import com.start.shop.modules.service.IGoodsService;
import com.start.shop.modules.service.IOrderService;
import com.start.shop.modules.service.IPayService;
import com.start.shop.modules.service.ISkuService;
import com.start.shop.modules.service.IUserService;

@RestController
@RequestMapping(value = "/order")
public class OrderController {

	@Autowired
	IGoodsService goodsService;

	@Autowired
	ISkuService skuService;

	@Autowired
	IOrderService orderService;

	@Autowired
	ICouponService couponService;
	
	@Autowired
	IUserService userService;
	
	@Autowired
	IPayService payService;

	
	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/订单服务
	 * @title 创建订单
	 * @description 创建订单
	 * @method post
	 * @url https://www.parpur.com/order/v1/orderMake
	 * @param custId true String 客户编号
	 * @param skuId true string 商品编号
	 * @param num true string 数量
	 * @param orderType true string 订单类型
     * @param addressId true int 地址id
     * @param couponId true int 优惠券ID
	 * @return {"code":"错误码","msg":"错误描述","data":{"orderNo":"xxxxxx"} }
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @return_param data orderNo 返回Json数据
	 * @remark orderNo:{orderNo:xxx}
	 * @number 1
	 */
	@RequestMapping(value = "/v1/orderMake")
	public ResponseEntity<MsgObject> make(@RequestBody @Valid OrderMakeParam orderMakeParam) {

		User user=userService.getUserById(orderMakeParam.getUser().getUserId());
		// 创建订单-仅供商城使用
		Order order = orderService.make(orderMakeParam.user.getUserId(), orderMakeParam.getSkuId(), orderMakeParam.getNum(),
			 orderMakeParam.getCouponId(), "shop", orderMakeParam.getAddress(), user.getAgentId());

		// 设置返回数据
		OrderVo orderVo = OrderVo.setData(order);

		return ResponseEntity.ok(MsgObject.success(orderVo));
	}
	

	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/订单服务
	 * @title 订单支付
	 * @description 对订单进行支付
	 * @method post
	 * @url https://www.parpur.com/order/v1/pay
	 * @param custId true String 客户编号
	 * @param orderNo true string 订单编号
	 * @return {"code":"错误码","msg":"错误描述"}
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @remark 
	 * @number 2
	 */
	@RequestMapping(value = "/v1/pay")
	public ResponseEntity<MsgObject> pay(@RequestBody @Valid PayParam payParam) {
		//订单支付
		payService.pay(payParam.user.getUserId(), payParam.getOrderNo(), payParam.getPayType(), payParam.getPassWord());
		//返回报文
		return ResponseEntity.ok(MsgObject.success(null));
	}

	/**
	 * 订单确认页面查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/v1/orderConfirm")
	public ResponseEntity<MsgObject> comfirm(@RequestBody @Valid OrderConfirmParam orderConfirmParam) {
		// 查询sku商品信息
		Sku sku = skuService.getSkuBySkuId(orderConfirmParam.getSkuId());

		// 查询商品信息
		Goods goods = goodsService.getGoodsByGoodsId(sku.getGoodsId());
		
		//查询商品类型
		GoodsType goodsType = goodsService.getGoodsTypeByTypeId(goods.getGoodsType());

		// 查询可用优惠券信息
		List<CouponVo> couponVoList = null;
//		List<CouponVo> couponVoList = couponService.getUseableCouponByOrder(orderConfirmParam.getCustId(),
//				sku.getSkuPrice().multiply(new BigDecimal(orderConfirmParam.getNum())), goods.getGoodsId(),
//				goods.getGoodsType());

		// 组织返回的商品信息
		OrderInfoVo orderInfoVo = OrderInfoVo.setData(sku, goods, goodsType, orderConfirmParam.getNum());

//		OrderConfirmVo orderComfirmVo = OrderConfirmVo.setdata(couponVoList, 1, "积分支付",
//				sku.getSkuPrice().multiply(new BigDecimal(orderConfirmParam.getNum())), orderInfoVo);

		return ResponseEntity.ok(MsgObject.success(null));
	}
	
	/**
	 * showdoc
	 * 
	 * @catalog 服务功能/订单服务
	 * @title 订单查询
	 * @description 查询所有的订单
	 * @method post
	 * @url https://www.parpur.com/order/v1/find
	 * @param custId true String 客户编号
	 * @param status true string 订单状态:0-未支付.1-已受理.2-已发货.3-已完成.
	 * @param orderType true string 订单类型
	 * @param pageSize true int 页面大小
	 * @param pageNum true int 当前页面
	 * @return {"code":"错误码","msg":"错误描述", data:OrderQueryVo}
	 * @return_param code string 错误码
	 * @return_param msg string 错误描述
	 * @remark ```OrderQueryVo{pageNum:xx, pageSize:xx, total:xx, orderVoList:[orderVoList] }  orderVoList{orderNo:xxx,goodsId:xxx, goodsType:xxx,goodsAmt:xxx, goodsCnt:xxx,totalAmt:xxx,couponId:xxx,couponAmt:xxx,actulAmt:xxx,addressId:xxx,payNo:xxx,goodsSendNo:xxx,createTime:xxx,}```
	 * @number 3
	 */
	
	@RequestMapping(value = "/v1/find")
	public ResponseEntity<MsgObject> orderQuery(@RequestBody @Valid OrderQueryParam orderQueryParam) {
		//获取对应状态的订单
		String orderStatus = null;
	    String payStatus = null;
	
	    switch (orderQueryParam.getStatus()) {
		case 0:    //全部
		    break;
		case 1:    //未支付
			orderStatus = "01"; //已创建
		    break;
		case 2:    //待发货
			orderStatus = "04"; //已受理
		    break;
		case 3:    //已发货
			    orderStatus = "05";  //已发货
		    break;
		case 4:    //已完成
		    orderStatus = "06"; //已收货
		    break;
		case 5:    //已关闭
		    orderStatus = "07"; //已撤销
		    break;
		default:
			break;
	    }
	    //查询订单
		PageInfo<Order> orderList = orderService.getOrderByStatus(orderQueryParam.user.getUserId(), orderStatus, payStatus, 
				orderQueryParam.getPageNum(), orderQueryParam.getPageSize(), orderQueryParam.getOrderChannel());

		//转化为外部的OrderVo对象
		OrderQueryVo orderQueryVo =OrderQueryVo.setData(orderList);

		//返回结果
		return ResponseEntity.ok(MsgObject.success(orderQueryVo));
	}
	
}
